﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Neo4jRestSharp.Neo4jRestServices
{  
    [Serializable]
    public class Neo4jException : Exception
    {
        /// <summary>
        /// hResult value for COR_E_APPLICATION
        /// </summary>
        const int hResult = -2146232832;
        string restMessage = "";

		public Neo4jException()
            : base()
        {
            base.HResult = hResult;
        }

        public Neo4jException(string message)
            : base(message)
        {
            base.HResult = hResult;
        }

        public Neo4jException(string message, Exception innerException)
            : base(message, innerException)
        {
            base.HResult = hResult;
        }
       
        public static implicit operator string(Neo4jException ex)
        {
            return ex.ToString();
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0}: {1}", this.GetType().FullName, this.Message);
            sb.AppendFormat(" The Neo4j rest call returned the following error message: '{0}'", this.restMessage);

            if (this.InnerException != null)
            {
                sb.AppendFormat(" ---> {0} <---", base.InnerException.ToString());
            }

            if (this.StackTrace != null)
            {
                sb.Append(Environment.NewLine);
                sb.Append(base.StackTrace);
            }

            return sb.ToString();
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("Neo4jException_RestMessage", this.restMessage, typeof(string));
        }    
    }
}
